﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DDD.Core;

using FloatComparer = DDDTest.Helper.FloatComparer;

namespace DDDTest.Core {
    [TestClass]
    public class TestFrameBuffer {
        [TestMethod]
        public void Test_New () {
            var fbuf = new FrameBuffer (2, 1, 1);

            Assert.AreEqual (2, fbuf.ColorBufferCount);
            Assert.AreEqual (0, fbuf.ColorBuffers.Count ());
            Assert.AreEqual (1, fbuf.DepthBufferCount);
            Assert.AreEqual (0, fbuf.DepthBuffers.Count ());
            Assert.AreEqual (null, fbuf.GetColorBuffer (0));
            Assert.AreEqual (null, fbuf.GetColorBuffer (1));
            Assert.AreEqual (null, fbuf.GetDepthBuffer ());
            Assert.AreEqual (null, fbuf.ColorBuffer);
            Assert.AreEqual (null, fbuf.DepthBuffer);
        }

        [TestMethod]
        public void Test_SetColorBuffer () {
            var fbuf = new FrameBuffer (2, 1, 1);
            var tex1 = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);
            var tex2 = new Image2D<byte> (PixelFormat.RGB8ui, 16, 16);

            fbuf.SetColorBuffer (0, tex1);
            fbuf.SetColorBuffer (1, tex2);
            Assert.AreEqual (2, fbuf.ColorBufferCount);
            Assert.AreEqual (2, fbuf.ColorBuffers.Count ());
            Assert.AreEqual (tex1, fbuf.ColorBuffer);
            Assert.AreEqual (tex1, fbuf.GetColorBuffer (0));
            Assert.AreEqual (tex2, fbuf.GetColorBuffer (1));

        }

        [TestMethod]
        public void Test_SetDepthBuffer () {
            var fbuf = new FrameBuffer (2, 1, 1);
            var img = new Image2D<float> (PixelFormat.Depth, 16, 16);
            fbuf.SetDepthBuffer (img);

            Assert.AreEqual (1, fbuf.DepthBufferCount);
            Assert.AreEqual (img, fbuf.DepthBuffer);
            Assert.AreEqual (img, fbuf.GetDepthBuffer ());
        }

        [TestMethod]
        public void Test_References () {
            var img1 = new Image2D<byte> (PixelFormat.RGBA8ui, 16, 16);
            var img2 = new Image2D<byte> (PixelFormat.RGBA8ui, 16, 16);
            var img3 = new Image2D<float> (PixelFormat.Depth, 16, 16);

            var fbuf = new FrameBuffer (2, 1, 1);
            fbuf.SetColorBuffer (0, img1);
            fbuf.SetColorBuffer (1, img2);
            fbuf.SetDepthBuffer (img3);

            var objs = fbuf.References;

            Assert.AreEqual (3, objs.Count ());
            Assert.AreEqual (true, objs.All (x => x != null));
        }

        [TestMethod]
        public void Test_Find () {
            var fbuf = new FrameBuffer (2, 1, 1);
            var img1 = new Image2D<byte> (PixelFormat.RGBA8ui, 16, 16);
            var img2 = new Image2D<byte> (PixelFormat.RGBA8ui, 16, 16);
            var img3 = new Image2D<float> (PixelFormat.Depth, 16, 16);
            fbuf.SetColorBuffer (0, img1);
            fbuf.SetColorBuffer (1, img2);
            fbuf.SetDepthBuffer (img3);

            img1.UserID = 101;
            img2.UserID = 102;
            img3.UserID = 103;
            fbuf.UserID = 104;

            Assert.AreEqual (img1, fbuf.Find (101));
            Assert.AreEqual (img2, fbuf.Find (102));
            Assert.AreEqual (img3, fbuf.Find (103));
            Assert.AreEqual (fbuf, fbuf.Find (104));
        }
    }
}
